//
//  ConstructionGround.m
//  TowerDefence
//
//  Created by K3 on 5/28/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "ConstructionGround.h"
#import "PlayScreen.h"

@implementation ConstructionGround 

-(id)initConstructionGroundWith:(GameLayer *)_gameLayer {
    self = [super initGameObjectWith:_gameLayer];
    [self loadResource];
    isShowTowerMenuTable = FALSE;
    
    return self;
}

-(void)loadResource {
    //draw ground and flag
    //we use CCMenu to create
    CCSprite *groundSprite = [CCSprite spriteWithFile:IMAGE_CONSTRUCTION_GROUND_BACKGROUND];    
    CCMenuItemSprite *groundMenuItem = [CCMenuItemSprite itemFromNormalSprite:groundSprite selectedSprite:nil target:self selector:@selector(constructionGroundPress:)];
    CCMenu *groundMenu = [CCMenu menuWithItems:groundMenuItem, nil];
    groundMenu.anchorPoint = ccp(0, 0);
    groundMenu.position = ccp(0, 0);
    [spriteBody addChild:groundMenu z:0 tag:TAG_CONSTRUCTION_GROUND_MENU];
    
    //add flag on ground construction
    CCSprite *flagSprite = [CCSprite spriteWithFile:IMAGE_CONSTRUCTION_GROUND_FLAG];
    [spriteBody addChild:flagSprite z:1 tag:TAG_CONSTRUCTION_GROUND_FLAG];
    flagSprite.position = ccp(spriteBody.textureRect.size.width/2, 10);
    
    /*
    //add four tower into towerMenuTable
    CCSprite *towerMenuTable = [CCSprite node];
    
    CCSprite *boomTowerMenuSprite = [CCSprite spriteWithFile:IMAGE_BOOM_TOWER_ITEM];
    CCSprite *boomTowerPrice = [CCSprite spriteWithFile:IMAGE_MINI_INFO_TABLE];
    boomTowerPrice.position = ccp(boomTowerMenuSprite.contentSize.width/2, 0);
    CCLabelTTF *boomTowerPriceLabel = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"%d", BOOM_TOWER_CONSTRUCTION_PRICE] fontName:FONT_COMIC_BOOK fontSize:11];
    boomTowerPriceLabel.position = ccp(boomTowerPrice.contentSize.width/2, boomTowerPrice.contentSize.height/2);
    boomTowerPriceLabel.color = ccc3(255, 254, 64);
    [boomTowerPrice addChild:boomTowerPriceLabel];
    [boomTowerMenuSprite addChild:boomTowerPrice];
    
    CCMenuItemSprite *boomTowerMenuItemImage = [CCMenuItemSprite itemFromNormalSprite:boomTowerMenuSprite selectedSprite:nil target:self selector:@selector(createBoomTower:)];
    CCMenu *boomTowerMenu = [CCMenu menuWithItems:boomTowerMenuItemImage, nil];
    boomTowerMenu.position = ccp(50, 50);
    
    CCSprite *arrowTowerMenuSprite = [CCSprite spriteWithFile:IMAGE_MAGIC_TOWER_ITEM];
    CCSprite *arrowTowerPrice = [CCSprite spriteWithFile:IMAGE_MINI_INFO_TABLE];
    arrowTowerPrice.position = ccp(arrowTowerMenuSprite.contentSize.width/2, 0);
    CCLabelTTF *arrowTowerPriceLabel = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"%d", MAGIC_TOWER_CONSTRUCTION_PRICE] fontName:FONT_COMIC_BOOK fontSize:11];
    arrowTowerPriceLabel.position = ccp(arrowTowerPrice.contentSize.width/2, arrowTowerPrice.contentSize.height/2);
    arrowTowerPriceLabel.color = ccc3(255, 254, 64);
    [arrowTowerPrice addChild:arrowTowerPriceLabel];
    [arrowTowerMenuSprite addChild:arrowTowerPrice];
    
    CCMenuItemSprite *arrowTowerMenuItemImage = [CCMenuItemSprite itemFromNormalSprite:arrowTowerMenuSprite selectedSprite:nil target:self selector:@selector(createArrowTower:)];
    CCMenu *arrowTowerMenu = [CCMenu menuWithItems:arrowTowerMenuItemImage, nil];
    arrowTowerMenu.position = ccp(-50, -50);
    
    CCSprite *barrackTowerMenuSprite = [CCSprite spriteWithFile:IMAGE_BARRACK_TOWER_ITEM];
    CCSprite *barrackTowerPrice = [CCSprite spriteWithFile:IMAGE_MINI_INFO_TABLE];
    barrackTowerPrice.position = ccp(barrackTowerMenuSprite.contentSize.width/2, 0);
    CCLabelTTF *barrackTowerPriceLabel = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"%d", BARRACK_TOWER_CONSTRUCTION_PRICE] fontName:FONT_COMIC_BOOK fontSize:11];
    barrackTowerPriceLabel.position = ccp(barrackTowerPrice.contentSize.width/2, barrackTowerPrice.contentSize.height/2);
    barrackTowerPriceLabel.color = ccc3(255, 254, 64);
    [barrackTowerPrice addChild:barrackTowerPriceLabel];
    [barrackTowerMenuSprite addChild:barrackTowerPrice];
    
    CCMenuItemSprite *barrackTowerMenuItemImage = [CCMenuItemSprite itemFromNormalSprite:barrackTowerMenuSprite selectedSprite:nil target:self selector:@selector(createBarrackTower:)];
    CCMenu *barrackTowerMenu = [CCMenu menuWithItems:barrackTowerMenuItemImage, nil];
    barrackTowerMenu.position = ccp(-50, 50);
    
    CCSprite *fastTowerMenuSprite = [CCSprite spriteWithFile:IMAGE_FAST_TOWER_ITEM];
    CCSprite *fastTowerPrice = [CCSprite spriteWithFile:IMAGE_MINI_INFO_TABLE];
    fastTowerPrice.position = ccp(fastTowerMenuSprite.contentSize.width/2, 0);
    CCLabelTTF *fastTowerPriceLabel = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"%d", FAST_TOWER_CONSTRUCTION_PRICE] fontName:FONT_COMIC_BOOK fontSize:11];
    fastTowerPriceLabel.position = ccp(fastTowerPrice.contentSize.width/2, fastTowerPrice.contentSize.height/2);
    fastTowerPriceLabel.color = ccc3(255, 254, 64);
    [fastTowerPrice addChild:fastTowerPriceLabel];
    [fastTowerMenuSprite addChild:fastTowerPrice];
    
    CCMenuItemSprite *fastTowerMenuItemImage = [CCMenuItemSprite itemFromNormalSprite:fastTowerMenuSprite selectedSprite:nil target:self selector:@selector(createFastTower:)];
    CCMenu *fastTowerMenu = [CCMenu menuWithItems:fastTowerMenuItemImage, nil];
    fastTowerMenu.position = ccp(50, -50);
    
    [towerMenuTable addChild:boomTowerMenu];
    [towerMenuTable addChild:arrowTowerMenu];
    [towerMenuTable addChild:barrackTowerMenu];
    [towerMenuTable addChild:fastTowerMenu];
    
    towerMenuTable.position = ccp(spriteBody.textureRect.size.width/2, spriteBody.textureRect.size.height/2);
    towerMenuTable.scale = 0;
    towerMenuTable.visible = NO;
    [spriteBody addChild:towerMenuTable z:1 tag:TAG_TOWER_MENU_TABLE];*/
}

-(void)constructionGroundPress:(id)sender {
    NSLog(@"hien65 thap");
    /*
    CCSprite *towerMenuTable = (CCSprite *)[spriteBody getChildByTag:TAG_TOWER_MENU_TABLE];
    
    if(!isShowTowerMenuTable) {
        isShowTowerMenuTable = TRUE;
        towerMenuTable.visible = YES;
        [towerMenuTable runAction:[CCSequence actions:[CCScaleTo actionWithDuration:0.25f scale:1.0f], nil]];
    } else {
        isShowTowerMenuTable = FALSE;
        [towerMenuTable runAction:[CCSequence actions:[CCScaleTo actionWithDuration:0.25f scale:0], [CCHide action], nil]];
    }*/
    
    [(PlayScreen *)handler constructionGroundPress:self];
}

-(void)createBoomTower:(id)sender {
    NSLog(@"xay thap");
    
    /*
    //Hide towerMenuTable
    isShowTowerMenuTable = FALSE;
    CCSprite *towerMenuTable = (CCSprite *)[spriteBody getChildByTag:TAG_TOWER_MENU_TABLE];
    [towerMenuTable runAction:[CCSequence actions:[CCScaleTo actionWithDuration:0.25f scale:0], [CCHide action], nil]];
    */
    
    [(PlayScreen*)handler createBoomTowerOnConstructionGround:self];
}

-(void)createArrowTower:(id)sender {
    NSLog(@"Xay thap arrow");
    
    /*
    //Hide towerMenuTable
    isShowTowerMenuTable = FALSE;
    CCSprite *towerMenuTable = (CCSprite *)[spriteBody getChildByTag:TAG_TOWER_MENU_TABLE];
    [towerMenuTable runAction:[CCSequence actions:[CCScaleTo actionWithDuration:0.25f scale:0], [CCHide action], nil]];
    */
    
    [(PlayScreen*)handler createMagicTowerOnConstructionGround:self];
}

-(void)createBarrackTower:(id)sender {
    NSLog(@"Xay thap arrow");
    
    /*
    //Hide towerMenuTable
    isShowTowerMenuTable = FALSE;
    CCSprite *towerMenuTable = (CCSprite *)[spriteBody getChildByTag:TAG_TOWER_MENU_TABLE];
    [towerMenuTable runAction:[CCSequence actions:[CCScaleTo actionWithDuration:0.25f scale:0], [CCHide action], nil]];
     */
    
    [(PlayScreen*)handler createBarrackTowerOnConstructionGround:self];
}

-(void)createFastTower:(id)sender {
    /*
    isShowTowerMenuTable = FALSE;
    CCSprite *towerMenuTable = (CCSprite *)[spriteBody getChildByTag:TAG_TOWER_MENU_TABLE];
    [towerMenuTable runAction:[CCSequence actions:[CCScaleTo actionWithDuration:0.25f scale:0], [CCHide action], nil]];
     */
    
    [(PlayScreen*)handler createFastTowerOnConstructionGround:self];
}

@end
